package com.photoeditor.demo.ui.widget.settings;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.app.TestUser;
import com.photoeditor.demo.model.preference.SPDataManager;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.activity.setting.MoreActivity;
import com.photoeditor.demo.util.Feedback;
/*import com.umeng.message.IUmengCallback;
import com.umeng.message.PushAgent;*/

/**
 * Created by Lai on 2018/4/9 0009.
 */

public class SettingView extends LinearLayout implements View.OnClickListener {

    SettingItem mShutterSound;
    SettingItem mNotification;
    SettingItem mSubscribe;
//    TextView mTvVersion;

    public SettingView(Context context) {
        super(context);
    }

    public SettingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SettingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mShutterSound = findViewById(R.id.first_setting_sound);
        mNotification = findViewById(R.id.first_setting_notification);
        mSubscribe = findViewById(R.id.first_setting_subscribe);
//        mTvVersion = findViewById(R.id.tv_version);

        mShutterSound.setOnClickListener(this);
        mNotification.setOnClickListener(this);
        mSubscribe.setOnClickListener(this);
        findViewById(R.id.first_setting_feedback).setOnClickListener(this);
        findViewById(R.id.first_setting_share).setOnClickListener(this);
        findViewById(R.id.first_setting_more).setOnClickListener(this);

        if (TestUser.isTestUser()) {
            mSubscribe.setVisibility(View.VISIBLE);
            mSubscribe.setCheck(SPDataManager.getIsSvipSwithOn());
        } else {
            mSubscribe.setVisibility(View.GONE);
        }

//        mTvVersion.setText("V " + AppUtils.getVersionName(ComponentContext.getContext()));
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            mShutterSound.setCheck(SPDataManager.isPlayShutterSound());
            mNotification.setCheck(SPDataManager.enableNotifacation());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_setting_sound:
                onSoundClicked();
                break;
            case R.id.first_setting_notification:
                onNotificationClicked();
                break;
            case R.id.first_setting_feedback:
                onFeedbackClicked();
                break;
            case R.id.first_setting_share:
                onShareClicked();
                break;
            case R.id.first_setting_more:
                onMoreClicked();
                break;
            case R.id.first_setting_subscribe:
                onSubscribeClicked();
                break;
            default:
                break;
        }
    }

    public void onSoundClicked() {
        boolean isCheck = mShutterSound.isCheck();
        mShutterSound.setCheck(!isCheck);
        SPDataManager.setPlayShutterSounde(!isCheck);
    }

    void onNotificationClicked() {
        boolean isCheck = !mNotification.isCheck();
        mNotification.setCheck(isCheck);
        enableNotification(isCheck);
    }

    public void onSubscribeClicked() {
        boolean isCheck = mSubscribe.isCheck();
        mSubscribe.setCheck(!isCheck);
        SPDataManager.setIsSvipSwithOn(!isCheck);
    }

    private void enableNotification(boolean enable) {
        SPDataManager.setEnableNotifacation(enable);
        /*PushAgent pushAgent = PushAgent.getInstance(ComponentContext.getContext());
        if (!enable) {
            pushAgent.disable(new IUmengCallback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onFailure(String s, String s1) {
                }
            });
        } else {
            pushAgent.enable(new IUmengCallback() {
                @Override
                public void onSuccess() {
                }

                @Override
                public void onFailure(String s, String s1) {
                }
            });
        }*/
    }

    void onFeedbackClicked() {
        Feedback.feedback(ComponentContext.getContext());
        StatisticsUtils.statisticsSettingClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_VALUE_FEEDBACK_CLICK);
        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_FEEDBACK_CLICK);
    }

    void onShareClicked() {
//        ShareAppDialogHelp dialogHelp = new ShareAppDialogHelp((Activity) getContext(), null);
//        dialogHelp.setFromPageId(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING);
//        dialogHelp.showShareDialog();
//        StatisticsUtils.statisticsSettingClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_VALUE_SHARE_CLICK);
//        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_TYF_CLICK);
    }

    void onMoreClicked() {
        Context context = ComponentContext.getContext();
        Intent i = new Intent(context, MoreActivity.class);
        context.startActivity(i);
        StatisticsUtils.statisticsSettingClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_VALUE_MORE_CLICK);
        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_MORE_CLICK);
    }

    //    @OnClick(R.id.first_setting_follow_ins)
//    void onFollowInsClicked() {
//        AppOpenUtil util = new AppOpenUtil(ComponentContext.getContext());
//        util.toInstagram(Global.INSTAGRAM_ID);
//        StatisticsUtils.statisticsSettingClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_VALUE_INS_CLICK);
//        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_IG_CLICK);
//    }

//    @OnClick(R.id.first_setting_follow_fb)
//    void onFollowFbClicked() {
//        AppOpenUtil util = new AppOpenUtil(ComponentContext.getContext());
//        util.toFacebook(Global.FB_ID);
//        StatisticsUtils.statisticsSettingClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_VALUE_FB_CLICK);
//        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_FB_CLICK);
//    }

//    @OnClick(R.id.first_setting_follow_twitter)
//    void onFollowTwitterClicked() {
//        AppOpenUtil util = new AppOpenUtil(ComponentContext.getContext());
//        util.toTwitter(Global.TWITTER_ID);
//        StatisticsUtils.statisticsSettingClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_VALUE_TWITTER_CLICK);
//        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_SETTING_TW_CLICK);
//    }

    //    @OnClick(R.id.first_setting_swipe)
//    void onSwipeClicked() {
//        Intent intent = new Intent(this, SwipeSettingActivity.class);
//        startActivity(intent);
//        StatisticsUtils.statisticsCustomCampageClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_CAMPAGE_VALUE_SWIPE_CLICK);
//    }

}
